package cn.wgx.commons.storage.fastdfs.proto.storage;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 下载为byte流
 * 
 * @author tobato
 *
 */
public class DownloadInputStream implements DownloadCallback<Long> {

    private OutputStream outputStream;

    public DownloadInputStream(OutputStream outputStream){
        this.outputStream = outputStream;
    }

    @Override
    public Long recv(InputStream ins) throws IOException {
        byte b[] = new byte[2048];
        long l = 0;
        int n = 0;
        BufferedInputStream bs = new BufferedInputStream(ins);
        while((n=bs.read(b)) > 0){
            l += b.length;
            outputStream.write(b,0, n);
            outputStream.flush();
        }
        return l;
    }
}
